package pl.iachoo.ctl.tabory.dao.hibernate;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.omg.CosNaming.IstringHelper;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import pl.iachoo.ctl.tabory.dao.api.services.ICompositionService;
import pl.iachoo.ctl.tabory.dao.api.services.ITripService;
import pl.iachoo.ctl.tabory.dao.api.services.IVagonService;
import pl.iachoo.ctl.tabory.dao.data.vo.TripVO;
import pl.iachoo.ctl.tabory.dao.data.vo.VagonVO;
import pl.iachoo.ctl.tabory.dao.hibernate.services.ServiceManager;

public class Activator implements BundleActivator {

	private final static Logger logg = Logger.getLogger(Activator.class);

	public void start(BundleContext context) throws Exception {
		logg.info("Starting bundle: " + context.getBundle().getSymbolicName());
		performTest();
	}

	public void stop(BundleContext context) throws Exception {
		logg.info("Stopping bundle: " + context.getBundle().getSymbolicName());
	}

	private void performTest() throws Exception {
		logg.info("----- TESTING -----");
		IVagonService vagonService = ServiceManager.getVagonService();
		ICompositionService compositionService = ServiceManager
				.getCompositionService();
		ITripService tripService = ServiceManager.getTripService();
		TripVO trip = new TripVO();
		trip.setDateFrom(new Date());
		trip.setDateUntil(new Date());
		trip.setDistanceKm(34.4);
		Set<VagonVO> setVagons = new HashSet<VagonVO>();
		for (VagonVO vagonVO : vagonService.getListAllVagons()) {
			setVagons.add(vagonVO);
		}
		trip.setSetVagons(setVagons);
		trip.setComposition(compositionService.getListAllCompositions().get(0));
		tripService.saveUpdateTrip(trip);
		// new SimpleTest();
		// IUserRoleService userRoleService =
		// ServiceManager.getUserRoleService();
		// try {
		// IVagonService vagonService = ServiceManager.getVagonService();
		// VagonVO newVagon = new VagonVO();
		// newVagon.setActive(true);
		// newVagon.setInternalId("11223");
		// newVagon.setRevisionCorrection(345.5);
		// newVagon.setRevisionDistance(4576.1);
		// newVagon.setRevOneFrom(new Date());
		// newVagon.setRevOneUntil(new Date());
		// newVagon.setRevTwoFrom(new Date());
		// newVagon.setRevTwoUntil(new Date());
		// vagonService.saveUpdateVagon(newVagon);
		// vagonService.getAllVagons();
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		logg.info("----- TESTING FINISHED -----");
	}

}
